package com.zhkj.txg.module.product.adapter

import android.view.View
import android.widget.ImageView
import com.chad.library.adapter.base.BaseProviderMultiAdapter
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.utils.ScreenUtil
import com.zhkj.txg.R
import com.zhkj.txg.utils.ImageUtils
import com.zhkj.txg.widget.CustomGSYVideoPlayer
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer

class ProductBannerAdapter : BaseProviderMultiAdapter<BannerUIEntity>() {

    init {
        addItemProvider(BannerVideoProvider())
        addItemProvider(BannerImageProvider())
        addItemProvider(BannerPullProvider())
    }

    override fun getItemType(data: List<BannerUIEntity>, position: Int): Int = data[position].type
}

class BannerVideoProvider : BaseItemProvider<BannerUIEntity>() {
    override val itemViewType: Int = BannerUIEntity.VIDEO
    override val layoutId: Int = R.layout.item_product_banner_video

    override fun convert(helper: BaseViewHolder, data: BannerUIEntity) {
        // 设置封面
        data.imageUrl?.let {
            val thumb = helper.getView<StandardGSYVideoPlayer>(R.id.productVideo).thumbImageView as ImageView
            ImageUtils.loadImage(context, thumb, it)
        }
        data.videoUrl?.let {
            helper.getView<CustomGSYVideoPlayer>(R.id.productVideo).setUpLazy(data.videoUrl, true, null, null, null)
        }
    }

    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        with(viewHolder.getView<StandardGSYVideoPlayer>(R.id.productVideo)) {
            layoutParams.height = ScreenUtil.getScreenWidth(context)
            thumbImageView = ImageView(context)
            thumbImageViewLayout.visibility = View.VISIBLE
            startButton.layoutParams.width = ScreenUtil.dip(context, 36)
            startButton.layoutParams.height = ScreenUtil.dip(context, 36)
            titleTextView.visibility = View.GONE
            backButton.visibility = View.GONE
            fullscreenButton.visibility = View.GONE
            setIsTouchWiget(false)
        }
    }
}

class BannerImageProvider : BaseItemProvider<BannerUIEntity>() {
    override val itemViewType: Int = BannerUIEntity.IMAGE
    override val layoutId: Int = R.layout.item_product_image

    override fun convert(helper: BaseViewHolder, data: BannerUIEntity) {
        data.imageUrl?.let {
            ImageUtils.loadImage(context, helper.getView(R.id.ivBannerView), it)
        }
    }
}

class BannerPullProvider : BaseItemProvider<BannerUIEntity>() {
    override val itemViewType: Int = BannerUIEntity.PULL
    override val layoutId: Int = R.layout.item_banner_pull

    override fun convert(helper: BaseViewHolder, data: BannerUIEntity) = Unit
}

data class BannerUIEntity(
    val type: Int,
    val videoUrl: String? = null,
    val imageUrl: String? = null
) {
    companion object {
        val VIDEO = 0
        val IMAGE = 1
        val PULL = 2
    }

    var userStop: Boolean = true
}